package top.carrotguo.weibo.widget;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import top.carrotguo.weibo.R;


/**
 * Created by carrot on 2018-10-12.
 */

public class LoadingDialog extends Dialog{


    public LoadingDialog(@NonNull Context context) {
        super(context);
    }

    public LoadingDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    public static class Builder{
        private Context mContext;
        private String message;
        private boolean canCancel = false;
        private boolean showMessage = false;
        private boolean isCancelOutside = false;

        public Builder(Context context){
            mContext = context;
        }

        public Builder setMessage(String msg){
            this.message = msg;
            return this;
        }

        public Builder setCancelable(boolean cancelable){
            canCancel = cancelable;
            return this;
        }

        public Builder setCancelOutside(boolean cancelOutside){
            isCancelOutside = cancelOutside;
            return this;
        }

        public Builder setShowMessage(boolean isShow){
            showMessage = isShow;
            return this;
        }

        public LoadingDialog create(){
            LayoutInflater inflater = LayoutInflater.from(mContext);
            View view = inflater.inflate(R.layout.dialog_loading,null);
            LoadingDialog loadingDialog = new LoadingDialog(mContext,R.style.Loading_Dialog_Style);
            TextView tvMessage = view.findViewById(R.id.tv_loading_text);
            if (showMessage) {
                tvMessage.setText(message);
            } else {
                tvMessage.setVisibility(View.GONE);
            }
            loadingDialog.setContentView(view);
            loadingDialog.setCancelable(canCancel);
            loadingDialog.setCanceledOnTouchOutside(isCancelOutside);
            return loadingDialog;
        }

    }
}
